//
//  Fraction.m
//  02-ClassObjective-C
//
//  Created by dllo on 16/7/5.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction


- (instancetype)initWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator {
    self = [super init];
    if (self) {
        _numerator = numerator;
        _denominator = denominator;
    }
    return self;
}
+ (instancetype)fractionWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator {

    return [[[Fraction alloc] initWithNumerator:numerator denominator:denominator] autorelease];
}
//求最大公约数
+ (NSInteger)maxGongYueFen:(Fraction *)fraction {
    while (fraction.numerator != 0) {
        NSInteger temp = fraction.denominator % fraction.numerator;
        fraction.denominator = fraction.numerator;
        fraction.numerator = temp;
    }
    return fraction.denominator;
}
//约分
+ (Fraction *)yueFen:(Fraction *)fraction{
   
    NSInteger maxGongYueShu = [Fraction maxGongYueFen:fraction];
    NSInteger newNumerator = fraction.numerator / maxGongYueShu;
    NSInteger newDenominatoer = fraction.denominator / maxGongYueShu;
    Fraction *newFraction = [[Fraction alloc] initWithNumerator:newNumerator denominator:newDenominatoer];
    return newFraction;
}
//比较大小
- (NSComparisonResult)compare:(Fraction *)fraction {
    NSInteger a = self.numerator * fraction.denominator;
    NSInteger b = fraction.numerator * self.denominator;
    if (a > b) {
        return NSOrderedDescending;
    }  else if(a < b){
        return NSOrderedAscending;
    }  else{
        return NSOrderedSame;
    }
}

//两个分数相加
- (Fraction *)AddOfFraction:(Fraction *)fraction {
    NSInteger numerator = self.numerator * fraction.denominator + self.denominator * fraction.numerator;
    NSInteger denominator = self.denominator * fraction.denominator;

    Fraction *newFraction = [[Fraction alloc] initWithNumerator:numerator denominator:denominator];
    return newFraction;
}

//两个分数相减
- (Fraction *)reductionOfFration:(Fraction *)fraction {
   NSInteger numerator = self.numerator * fraction.denominator - self.denominator * fraction.numerator;
    NSInteger denominator = self.denominator * fraction.denominator;
    
    Fraction *newFraction = [Fraction fractionWithNumerator:numerator denominator:denominator];
    return newFraction;
  }

//两个分数相乘
- (Fraction *)multiplyOfFraction:(Fraction *)fraction {
    NSInteger numerator = self.numerator * fraction.numerator;
    NSInteger denominator = self.denominator * fraction.denominator;
    Fraction *newFraction = [Fraction fractionWithNumerator:numerator denominator:denominator];
   return newFraction;
}

//两个分数相除
- (Fraction *)divideOfFraction:(Fraction *)fraction {
    NSInteger numerator = self.numerator * fraction.denominator;
    NSInteger denominator = self.denominator * fraction.numerator;
    Fraction *newFraction = [Fraction fractionWithNumerator:numerator denominator:denominator];
    return newFraction;
}
- (NSString *)description {
    return [NSString stringWithFormat:@"%ld/%ld", (long)self.numerator, (long)self.denominator];
}



@end
